﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Summary description for Category_Session
/// </summary>
public class Category_Session
{
    private static SqlConnection sqlConnection;
    public static SqlDataAdapter sqlAdapter;
    private static DataTable objDataTable;
    public static SqlCommand sqlCommand; 

	public Category_Session()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static List<Category> GetList_Category()
    {
        DataTable dt = new DataTable();
        dt = DataProvider.GetDataTable("select * from Category");
        List<Category> lst = new List<Category>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Category temp = new Category();
            temp.Cate_ID = (dt.Rows[i]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Cate_ID"];
            temp.Cate_Name = (dt.Rows[i]["Cate_Name"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Cate_Name"];
            temp.Cate_Status = (dt.Rows[i]["Cate_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[i]["Cate_Status"];
            temp.Cate_Links = (dt.Rows[i]["Cate_Links"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Cate_Links"];
            temp.Cate_Image = (dt.Rows[i]["Cate_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Cate_Image"];
            temp.Cate_ParentID = (dt.Rows[i]["Cate_ParentID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Cate_ParentID"];
            lst.Add(temp);
        }
        return lst;
    }
    public static DataTable Get_CategoryAsTable(string whereSql, string orderBy)
    {
        DataTable dt = DataProvider.GetDataTable("select * from Category where " + whereSql );
        //WebMsgBox.Show(dt.Rows.Count.ToString());
        return dt;
    }
    public static Category Get_Category_ByID(System.Int32 Cate_ID)
    {
        DataTable dt = DataProvider.GetDataTable("select * from Category where Cate_ID =" + Cate_ID);
        Category temp = new Category();
        temp.Cate_ID = (dt.Rows[0]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Cate_ID"];
        temp.Cate_Name = (dt.Rows[0]["Cate_Name"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Cate_Name"];
        temp.Cate_Status = (dt.Rows[0]["Cate_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[0]["Cate_Status"];
        temp.Cate_Links = (dt.Rows[0]["Cate_Links"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Cate_Links"];
        temp.Cate_Image = (dt.Rows[0]["Cate_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Cate_Image"];
        temp.Cate_ParentID = (dt.Rows[0]["Cate_ParentID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Cate_ParentID"];
        return temp;
    }

    public static void Delete_Category(Category temp)
    {
        String strSQL;
        SqlCommand cmd = null;
        try
        {
            //de xoa dc pro thi ta phai tim dc id cua no va xoa comment va xoa basket
            // 1) tim Pro_ID bi xoa
            DataTable dt = DataProvider.GetDataTable("select * from Product where Cate_ID =  " + temp.Cate_ID);
           
            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                    Product_Session.Delete_Product(int.Parse(dt.Rows[i]["Pro_ID"].ToString()));
            }
            // xoa Basket co News lien quan
            sqlConnection = new SqlConnection();
            sqlConnection = DataProvider.CreateConnection();
            strSQL = "delete  from News where Cate_ID = " + temp.Cate_ID;
            cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();

            // xoa Basket co Advertise lien quan
            strSQL = "delete  from Advertise where Cate_ID = " + temp.Cate_ID;
            cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();

            // xoa Basket co Advertise lien quan
            strSQL = "delete  from Category where Cate_ID = " + temp.Cate_ID;
            cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();


            sqlConnection.Close();
            cmd = null;
        }
        catch (Exception e) {
            WebMsgBox.Show("ngoai le vang ra ne");
        }

    }
    public static void Update_Category(Category temp)
    {
        sqlConnection = new SqlConnection();
        sqlConnection = DataProvider.CreateConnection();


        String strSQL;

        strSQL = "UPDATE Category  SET  Cate_Name = @Cate_Name , Cate_Status = @Cate_Status,Cate_Links = @Cate_Links  WHERE Cate_ID= @Cate_ID ";
        SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection);
        sqlCommand.CommandType = CommandType.Text;

        sqlCommand.CommandText = strSQL;
        sqlCommand.Parameters.Add("@Cate_ID", SqlDbType.Int).Value = temp.Cate_ID;
        sqlCommand.Parameters.Add("@Cate_Name", SqlDbType.NVarChar).Value = temp.Cate_Name;
        sqlCommand.Parameters.Add("@Cate_Status", SqlDbType.Bit).Value = temp.Cate_Status;
        sqlCommand.Parameters.Add("@Cate_Links", SqlDbType.VarChar).Value = temp.Cate_Links;

        sqlCommand.ExecuteNonQuery();// tai sao bi bao loi
        sqlCommand.Dispose();
        sqlCommand = null;
    }
    public static void Insert_Category(Category temp)
    {
        sqlConnection = DataProvider.CreateConnection();
       
        //================================== Thỏa mãn các điều kiện và thêm vào ============================//
        DataTable datatable = new DataTable();
        sqlAdapter = new SqlDataAdapter("select * from Category", sqlConnection);
        sqlAdapter.Fill(datatable);

        DataRow datarow = datatable.NewRow();
        datarow[1] = temp.Cate_Name;
        datarow[2] = temp.Cate_Status;
        datarow[3] = temp.Cate_Links;
        datarow[4] = temp.Cate_Image;
        datarow[5] = temp.Cate_ParentID;

        datatable.Rows.Add(datarow);

        sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandType = CommandType.Text;
        sqlCommand.CommandText = "INSERT INTO Category VALUES(@Cate_Name,@Cate_Status,@Cate_Links,@Cate_Image,@Cate_ParentID)";
        sqlCommand.Parameters.Add("@Cate_Name", SqlDbType.NVarChar, 50, "Cate_Name");
        sqlCommand.Parameters.Add("@Cate_Status", SqlDbType.Bit, 1, "Cate_Status");
        sqlCommand.Parameters.Add("@Cate_Links", SqlDbType.NVarChar, 50, "Cate_Links");
        sqlCommand.Parameters.Add("@Cate_Image", SqlDbType.NVarChar, 100, "Cate_Image");
        sqlCommand.Parameters.Add("@Cate_ParentID", SqlDbType.Int, 10, "Cate_ParentID");

        sqlAdapter.InsertCommand = sqlCommand;
        sqlAdapter.Update(datatable);
        sqlCommand.Dispose();
        sqlCommand = null;
    }

}
